package com.hongyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.SysOperLog;
import com.hongyi.system.domain.dto.SysOperLogDto;
import com.hongyi.system.domain.vo.SysOperLogVo;
import com.hongyi.system.service.ISysOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志控制器
 *
 * @author DingHao
 */
@ApiSort(value = 13)
@Api(tags = "操作日志控制器")
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperLogController extends BaseController {

    @Autowired
    private ISysOperLogService sysOperLogService;

    /**
     * 操作日志分页数据
     *
     * @param sysOperLogDto 操作日志对象
     * @param pageQuery 分页数据
     * @return 分页数据
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "操作日志分页数据")
    @ApiImplicitParam(name = "operation" , value = "操作日志对象" , dataTypeClass = SysOperLog.class, paramType = "query")
    @SaCheckPermission("monitor:operlog:list")
    @GetMapping(value = "/list")
    public PageData<SysOperLogVo> list(SysOperLogDto sysOperLogDto, PageQuery pageQuery) {
        return sysOperLogService.selectPageOperLogList(sysOperLogDto, pageQuery);
    }

    /**
     * 操作日志导出
     *
     * @param sysOperLogDto 日志对象
     * @param response  返回
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "操作日志导出")
    @Log(title = "操作日志导出" , businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:operlog:export")
    @PostMapping(value = "/exportByStream" , produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportByStream(SysOperLogDto sysOperLogDto, HttpServletResponse response) {
        List<SysOperLogVo> sysOperLogVos = sysOperLogService.selectOperLogList(sysOperLogDto);
        ExcelUtil.exportExcel(sysOperLogVos, "操作日志", SysOperLogVo.class, response);

    }

    /**
     * 操作日志删除
     *
     * @param ids 操作日志ID数组
     * @return 结果
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "操作日志删除")
    @ApiImplicitParam(name = "ids" , value = "操作日志ID数组" , paramType = "path" , allowMultiple = true, required = true)
    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/{ids}")
    public JsonResult<Void> remove(@PathVariable Long[] ids) {
        return toResult(sysOperLogService.deleteOperLogByIds(ids));
    }

    /**
     * 操作日志清除
     *
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "操作日志清除")
    @Log(title = "操作日志" , businessType = BusinessType.CLEAN)
    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/clean")
    public JsonResult<Void> clean() {
        sysOperLogService.cleanOperLog();
        return JsonResult.ok();
    }

}
